/**
 * 
 */
package userLogic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import reservationEntity.ReservationEntity;
import userController.Navigator;
import userEntity.CustomerBookingDisplayable;
import userEntity.MembershipEntity;
import userService.CustomerService;
import vehicleEntity.SaleVehicleEntity;

/**
 * @author Yokesh Kumar
 *
 */
public class CustomerLogic {

	/**
	 * This method fetches the reservation details of the customer
	 * @param phoneNumber
	 * @return
	 */
	public ObservableList<CustomerBookingDisplayable> fetchCustomerBookingDetails(){
		CustomerService service = new CustomerService();
		ResultSet reserveRs= null;
		ResultSet rentRs = null;
		List<CustomerBookingDisplayable> customerDispList = new ArrayList<CustomerBookingDisplayable>();
		ObservableList<CustomerBookingDisplayable> observableCustDispList = null;
		CustomerBookingDisplayable bookdisp = null;
		try{
			reserveRs = service.fetchReservationDetails(Navigator.getPhoneNumber());
			rentRs = service.fetchRentDetails(Navigator.getPhoneNumber());
			if(reserveRs != null){
				while(reserveRs.next()){
					bookdisp = new CustomerBookingDisplayable();
					bookdisp.setReservationId(reserveRs.getInt("reservationId"));
					bookdisp.setReservationDate(reserveRs.getDate("reservationDate"));
					bookdisp.setStatus(reserveRs.getString("status"));
					customerDispList.add(bookdisp);
				}
			}
			if(rentRs != null){
				while(rentRs.next()){
					bookdisp = new CustomerBookingDisplayable();
					bookdisp.setReservationId(rentRs.getInt("reservationId"));
					bookdisp.setReservationDate(rentRs.getDate("reservationDate"));
					bookdisp.setStatus(rentRs.getString("status"));
					bookdisp.setRentId(rentRs.getInt("rentId"));
					bookdisp.setRentDate(rentRs.getDate("rentDate"));
					bookdisp.setRentTime(rentRs.getTime("rentTime"));
					bookdisp.setReturnDate(rentRs.getDate("returnDate"));
					bookdisp.setReturnTime(rentRs.getTime("returnTime"));
					bookdisp.setVehicleName(rentRs.getString("vehicleName"));
					bookdisp.setFinalCost(rentRs.getDouble("finalCost"));
					customerDispList.add(bookdisp);
				}
			}
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		observableCustDispList = FXCollections.observableList(customerDispList);
		service.closeConnection();
		return observableCustDispList;
	}
	
	/**
	 * This method fetches the rent details of the customer
	 * @param phoneNumber
	 * @return
	 */
	public List<ReservationEntity> fetchRentDetails(String phoneNumber){
		
		return null;
	}
	
	/**
	 * This method fetches the membership details of the customer
	 * @param phoneNumber
	 * @return
	 */
	public MembershipEntity fetchMembershipDetails(String phoneNumber){
		ResultSet isMemberRs = null;
		ResultSet memberRs = null;
		MembershipEntity membership = null;
		CustomerService service = new CustomerService();
		boolean isMember = false;
		try{
			isMemberRs = service.checkForMembership(phoneNumber);
		
		if(isMemberRs.next()){
			isMember = isMemberRs.getBoolean("isMember");
			if(isMember){
				memberRs = service.fetchMembershipDetails(phoneNumber);
				if(memberRs.next()){
					membership = new MembershipEntity();
					membership.setMemberShipId(memberRs.getInt("membershipId"));
					membership.setJoinDate(memberRs.getDate("joinDate"));
					membership.setPoints(memberRs.getInt("points"));
					membership.setExpirydate(memberRs.getDate("expiryDate"));
					membership.setStatus(memberRs.getString("status"));
				}
				
			}
		}
		else{
			throw new Exception();
		}
		}
		catch(SQLException sqlExep){
			sqlExep.printStackTrace();
		}
		catch (Exception e) {
			System.out.println("value not found in login table");
		}
		service.closeConnection();
		return membership;
	}
}
